<?php
/**
 *
 * This file contains {@link Domain51_Service_Delicious}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Util
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */

/**
 * Load required super class
 */
Solar::loadClass('Solar_Base');


/**
 *
 * This class provides a basic connection class for HTTP-Auth based services.
 *
 * <i>Requires</i> {@link http://pecl.php.net/package/pecl_http pecl_http}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Util
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 * @todo rename class so HTTP-Auth is indicated in name
 * @todo allow configuration of return object for doRequest
 * @todo refactor out dependence on pecl_http by moving connection code from
 *       doRequest() into another object.
 *
 */
class Domain51_Util_Service_Connection extends Solar_Base
{
    /**
     * User-provided configuration values
     *
     * Keys are:
     * `username`
     * : (string) The service_url's username
     *
     * `password`
     * : (string) The service_url's password
     *
     * `service_url`
     * : (string) The URL to make requests from
     *
     *
     * @var     array
     * @access  protected
     */
    protected $_Domain51_Util_Service_Connection = array(
        'username'    => '',
        'password'    => '',
        'service_url' => '' 
    );
    
    
    /**
     *
     * Handle instantiation
     *
     *
     * @param array $config 
     *
     */
    public function __construct($config = null) {
        parent::__construct($config);
    }
    
    
    /**
     *
     * Performs a GET request executing the supplied URI at the configured
     * service_url.
     *
     *
     * @param string $uri URI to execute
     *
     * @return SimpleXMLElement
     *
     * @todo refactor HTTPRequest init/send into another object
     *
     */
    public function doRequest($uri) {
        $options = array(
            'httpauth' => "{$this->_config['username']}:{$this->_config['password']}",
            'httpauthtype' => HTTP_AUTH_BASIC
        );
        
        $request = new HTTPRequest();
        $request->setOptions($options);
        $request->setUrl($this->_config['service_url'] . $uri);
        
        $response = $request->send();
        return new SimpleXMLElement($response->getBody());
    }
}

?>